﻿// <copyright file="XmlNodeWriter.cs" company="Signal Processing Lab, Wuhan University">
// Copyright (c) 2009-2010 All Right Reserved
// </copyright>
// <author>Xin-ping Deng</author>
// <email>dentoby@gmail.com</email>
// <date>2010-4</date>
// <summary>This is part of Image.NET project.</summary>

namespace SPLWHU.Core
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Xml;

    /// <summary>
    /// The xml node writer.
    /// </summary>
    public class XmlNodeWriter
    {
        /// <summary>
        /// The attached xml node.
        /// </summary>
        private XmlElement node;

        /// <summary>
        /// The xml document.
        /// </summary>
        private XmlDocument xmlDoc;

        /// <summary>
        /// Initializes a new instance of the <see cref="XmlNodeWriter"/> class.
        /// </summary>
        /// <param name="name">The name of the node.</param>
        public XmlNodeWriter(string name)
        {
            this.xmlDoc = new XmlDocument();
            this.node = this.xmlDoc.CreateElement(name);
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="XmlNodeWriter"/> class.
        /// </summary>
        /// <param name="node">The xml node.</param>
        /// <param name="doc">The xml document.</param>
        private XmlNodeWriter(XmlElement node, XmlDocument doc)
        {
            this.node = node;
            this.xmlDoc = doc;
        }

        /// <summary>
        /// Saves the xml node to file.
        /// </summary>
        /// <param name="filename">The filename.</param>
        public void SaveToFile(string filename)
        {
            this.xmlDoc.Save(filename);
        }

        /// <summary>
        /// Creates a child node.
        /// </summary>
        /// <param name="name">The name of the child node.</param>
        /// <returns>The <see cref="XmlNodeWriter"/> of the child node.</returns>
        public XmlNodeWriter CreateChild(string name)
        {
            XmlElement ch = this.xmlDoc.CreateElement(name);
            this.node.AppendChild(ch);

            return new XmlNodeWriter(ch, this.xmlDoc);
        }

        /// <summary>
        /// Adds an attribute to the node.
        /// </summary>
        /// <param name="name">The name of the attribute.</param>
        /// <param name="value">The value of the attribute.</param>
        public void AddAttribute(string name, string value)
        {
            this.node.SetAttribute(name, value);
        }

        /// <summary>
        /// Sets the value of this node.
        /// </summary>
        /// <param name="value">The value of this node.</param>
        public void SetValue(string value)
        {
            XmlText xmlText = this.xmlDoc.CreateTextNode(value);
            this.node.AppendChild(xmlText);
        }
    }
}
